package com.hbpu.encap.thread;

import java.util.Scanner;

public class Homework01 {
    public static void main(String[] args) throws InterruptedException {
        Thread01 thread01 = new Thread01();
        Thread02 thread02 = new Thread02(thread01);
        thread01.start();
        thread02.start();

    }
}

class Thread01 extends Thread {
    private boolean flag = true;
    private int i = 0;
    @Override
    public void run() {
        while (flag) {
                System.out.println(i);
                i++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
        }
        System.out.println("Thread01 end");
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
class Thread02 extends Thread {
    private final Thread01 thread01;
    private Scanner scanner = new Scanner(System.in);

    public Thread02(Thread01 thread01) {
        this.thread01 = thread01;
    }

    @Override
    public void run() {
        while (true) {
            System.out.print("请输入字符：");
            char sc = scanner.next().charAt(0);
            if (sc == 'Q') {
                thread01.setFlag(false);
                break;
            }
        }
        System.out.println("Thread02 end");
    }
}
